<?php
/**
 * @title 376. 摆动序列 - 最长上摆序列-最长下摆序列
 * @author start2004
 */


class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function wiggleMaxLength($nums) {
        /**
         * @since 2020-12-14 长度
         */
        $len = count($nums);
        if($len == 0){
            return 0;
        } else {}

        /**
         * @since 2020-12-16 最长上摆序列, 最长下摆序列
         */
        $up = $down = 1;
        for($i=1; $i<$len; $i++){
            if($nums[$i] > $nums[$i-1]){
                $up = max($up, $down+1);
            } elseif($nums[$i] < $nums[$i-1]) {
                $down = max($down, $up+1);
            } else {
                /**
                 * @since 2020-12-16 相同不处理
                 */
            }
        }

        /**
         * @return
         */
        return max($up, $down);
    }
}




/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.03_5.php
 */
$datas = [
    [1,7,4,9,2,5], // 6
    [1,1,1,], // 1
    [1,17,5,10,13,15,10,5,16,8], // 7
    [1,2,3,4,5,6,7,8,9], // 2
    [1,1,1,1,2], // 2
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->wiggleMaxLength($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
